package com.meichis.yndms.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.meichis.yndms.R;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityClientDetailBinding;
import com.meichis.yndms.model.ClientInfo;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.ClientDetailVM;

public class ClientDetailActivity extends BaseActivity {

    private ActivityClientDetailBinding binding;
    private ClientDetailVM vm;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_client_detail);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ClientDetailVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("店铺详情");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        vm.setDMSID(getIntent().getStringExtra(MCode.DMSID));
        vm.setClientInfo((ClientInfo) getIntent().getSerializableExtra(MCode.CLIENT));
        addObserver();
        vm.GetShopInfo();
        vm.GetTerminalLastVisit();


    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
    }


    public class ClickEvent {
        public void onTelAction() {
            //跳转到拨号界面，同时传递电话号码
            String tel = vm.getShopInfo().getValue().getMobile().contains("+86") ?
                    vm.getShopInfo().getValue().getMobile().replace("+86", "")
                    : vm.getShopInfo().getValue().getMobile();
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tel));
            startActivity(intent);
        }

        public void onVst(){
            Bundle bundle = new Bundle();
            bundle.putSerializable(MCode.CLIENT, vm.getClientInfo());
            bundle.putSerializable(MCode.ShopInfo, vm.getShopInfo().getValue());
            openActivity(VST_TemplateActivity.class, bundle);
        }
    }
}
